package com.sxfq.loansupermarket.backsystem.modular.tcCenter.controller;

import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.FirstCustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.ScoreDealInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerDetailsService;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.ConsultantInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.DepartmentInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.NurseInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.IConsultantDetailsService;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.IConsultantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/consultantDetails")
public class ConsultantDetailsController extends BaseController {
    @Autowired
    IConsultantService service;

    @Autowired
    ICustomerDetailsService customerDetailsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    IConsultantDetailsService consultantDetailsService;

    private String PREFIX = "/tcCenter/consultant/";

    //tc点击人名跳转
    @RequestMapping(value = "/consultantToDetailByTab/{id}/{tabId}/{isKz}")
    public String consultantToDetailByTab(@PathVariable String id, @PathVariable String tabId, @PathVariable("isKz") String isKz, Model model) throws Exception {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        CustomerInfo customerInfo = customerDetailsService.selectOneById(id); //当前记录的患者信息
        FirstCustomerInfo firstCustomerInfo = customerDetailsService.firstVisitInformation(customerInfo.getReservationsId()); //首次到店的患者信息
        customerInfo.setOrthodonticNumber(customerDetailsService.getorthodonticNumber(customerInfo.getReservationsId()));
        customerDetailsService.getCustomerInformationsList();
        customerInfo.setFirstAppointmentPayService(firstCustomerInfo.getAppointmentPayService());
        customerInfo.setFirstArchiving(firstCustomerInfo.getArchiving());
        customerInfo.setFirstArrivalTime(firstCustomerInfo.getArrivalTime());
        customerInfo.setFirstFieldTc(firstCustomerInfo.getFieldTc());
        ReservationsInfo reservationsInfo = customerDetailsService.getReservationsInfoList(customerInfo.getReservationsId());
        customerDetailsService.intodayOperation(customerInfo);
        if(reservationsInfo==null){
            model.addAttribute("customerInfo", customerInfo);
            model.addAttribute("tabId", tabId);
            model.addAttribute("isKz", isKz);
            model.addAttribute("userName", ShiroKit.getUser().getAccount());
            model.addAttribute("roleNameLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"RoleName",0));
            model.addAttribute("dealyesLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Dealyes",0));
            model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
            model.addAttribute("isRefundAdmin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"IsRefundAdmin",0));
            model.addAttribute("lowestPriceBill", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"LowestPriceBill",0)+"");
            model.addAttribute("occupationList", ShiroKit.getUser().getOccupationInfoList());
            model.addAttribute("refereeShipList", ShiroKit.getUser().getRefereeShipInfoList());
            return PREFIX + "consultant_Details.html";
        } else {
            model.addAttribute("customerInfo", customerInfo);
            model.addAttribute("reservationsInfo", reservationsInfo);
            model.addAttribute("tabId", tabId);
            model.addAttribute("isKz", isKz);
            model.addAttribute("userName", ShiroKit.getUser().getAccount());
            model.addAttribute("roleNameLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"RoleName",0));
            model.addAttribute("dealyesLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Dealyes",0));
            model.addAttribute("outpatientName", ShiroKit.getUser().getOutpatientName());
            model.addAttribute("isRefundAdmin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"IsRefundAdmin",0));
            model.addAttribute("lowestPriceBill", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"LowestPriceBill",0)+"");
            model.addAttribute("occupationList", ShiroKit.getUser().getOccupationInfoList());
            model.addAttribute("refereeShipList", ShiroKit.getUser().getRefereeShipInfoList());
            return PREFIX + "consultant_Detailssc.html";
        }
    }

    //tc开单操作
    @RequestMapping(value = "/orderOperation", method = RequestMethod.POST)
    @ResponseBody
    public ScoreDealInfo orderOperation(String reservationsId) {
        ScoreDealInfo scoreDealInfo = consultantDetailsService.orderOperation(reservationsId);
        scoreDealInfo.setMoneyBalance(consultantDetailsService.getMoneyBalance(reservationsId));
        scoreDealInfo.setJfbalance(consultantDetailsService.getJfbalance(reservationsId));
        scoreDealInfo.setLowestPriceBill(Integer.parseInt(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"LowestPriceBill",0)+""));
        VipLevelInfo vipLevelInfo = consultantDetailsService.getVipLevel(reservationsId);
        scoreDealInfo.setVipLevelName(vipLevelInfo.getVipLevelName());
        scoreDealInfo.setDiscountDiscount(vipLevelInfo.getDiscountDiscount());
        scoreDealInfo.setGetDepartmentList(consultantDetailsService.getDepartmentList());
        scoreDealInfo.setGetNurseList(consultantDetailsService.getNurseList());
        scoreDealInfo.setGetDoctorList(consultantDetailsService.getDoctorList());
        scoreDealInfo.setObtainDeductionMethod(consultantDetailsService.getObtainDeductionMethod());
        return scoreDealInfo;
    }

    //开单记录
    @RequestMapping(value = "/orderDealListTc", method = RequestMethod.POST)
    @ResponseBody
    public List<PayServicedInfo> orderDealListTc(String reservationsId) {
        List<PayServicedInfo> payServiceInfoListS = consultantDetailsService.orderDealListTc(reservationsId);
        return payServiceInfoListS;
    }

    //获取抵扣方式
    @RequestMapping(value = "/getObtainDeductionMethod", method = RequestMethod.POST)
    @ResponseBody
    public String getObtainDeductionMethod() {
        return consultantDetailsService.getObtainDeductionMethod();
    }

    //看诊/回访
    @RequestMapping(value = "/diagnoseTc", method = RequestMethod.POST)
    @ResponseBody
    public ConsultantInfo diagnoseTc(String reservationsId) {
        ConsultantInfo infoList = consultantDetailsService.diagnoseTc(reservationsId);
        infoList.setGetTcList(consultantDetailsService.getTcList());
        infoList.setGetSfList(consultantDetailsService.getSfList());
        infoList.setGetJgList(consultantDetailsService.getJgList());
        return infoList;
    }

    //查科室
    @RequestMapping(value = "/getDepartmentList", method = RequestMethod.POST)
    @ResponseBody
    public List<DepartmentInfo> getDepartmentList() {
        List<DepartmentInfo> departmentList = consultantDetailsService.getDepartmentList();
        return departmentList;
    }

    //查医生
    @RequestMapping(value = "/getDoctorList", method = RequestMethod.POST)
    @ResponseBody
    public List<DoctorInfo> getDoctorList() {
        List<DoctorInfo> doctorList = consultantDetailsService.getDoctorList();
        return doctorList;
    }

    //查护士
    @RequestMapping(value = "/getProjectNurse", method = RequestMethod.POST)
    @ResponseBody
    public List<NurseInfo> getProjectNurse() {
        List<NurseInfo> nurseList = consultantDetailsService.getNurseList();
        return nurseList;
    }

    //查Tc
    @RequestMapping(value = "/getTcList", method = RequestMethod.POST)
    @ResponseBody
    public List<TcInfo> getTcList() {
        List<TcInfo> tcList = consultantDetailsService.getTcList();
        return tcList;
    }
}